<template>
  <div>
    <div class="mb-2">
      <ViewColumns
        :formCode="formCode"
        :moduleCode="moduleCode"
        @success="handleSuccess"
        ref="viewColumnsRef"
        :headStyle="headStyle"
      />
    </div>
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
      <template #tableTitle>
        <div :style="{ ...headStyle, padding: '0 24px', fontSize: '16px' }"> 预览列表 </div>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
        <a-button type="primary" @click="handleCreateMappingField(1)"> 新增映射字段 </a-button>
        <a-button type="primary" @click="handleCreateMappingField(2)"> 新增多重映射字段 </a-button>
      </template>
      <template #title="{ record }">
        <div v-html="record.title" v-if="record.title"> </div>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'leftTop',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <ViewListDrawer @register="registerDrawer" @success="handleSuccess" />
    <MappingFieldsDrawer @register="registerMappingFieldsDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getVisualGetColsForSetup } from '/@/api/formDesign/formDesign';
  import { useDrawer } from '/@/components/Drawer';
  import ViewListDrawer from './ViewListDrawer.vue';
  import MappingFieldsDrawer from './MappingFieldsDrawer.vue';
  import ViewColumns from './viewColumns/index.vue';
  import { columns } from './viewList.data';
  import { getVisualColDel } from '/@/api/module/module';

  export default defineComponent({
    name: 'ViewList',
    components: { BasicTable, ViewListDrawer, TableAction, ViewColumns, MappingFieldsDrawer },
    props: {
      formCode: {
        type: String,
        default: '',
      },
      moduleCode: {
        type: String,
        default: '',
      },
      headStyle: {
        type: Object,
        default: () => {
          return {};
        },
      },
    },
    setup(props) {
      const viewColumnsRef = ref<any>(null);

      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerMappingFieldsDrawer, { openDrawer: openMappingFieldsDrawer }] = useDrawer();

      onMounted(async () => {});

      const [registerTable, { reload, getDataSource }] = useTable({
        title: '',
        api: getVisualGetColsForSetup,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: [],
        },
        searchInfo: {
          op: 'search',
        }, //额外的参数
        useSearchForm: false,
        showTableSetting: false,
        bordered: true,
        indexColumnProps: { width: 50 },
        pagination: false,
        showIndexColumn: true,
        canResize: false,
        beforeFetch: (info) => {
          let newInfo = info;
          newInfo.moduleCode = props.moduleCode;
          return newInfo;
        },
        afterFetch: (data) => {
          data.forEach((item) => {
            item.fieldTitle = item.fieldTitle == '#' ? '' : item.fieldTitle;
            item.link = item.link == '#' ? '' : item.link;
            item.fieldGroup = item.fieldGroup == '#' ? '' : item.fieldGroup;
          });
          return data;
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      //普通新增
      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
          dataSourceLength: getDataSource().length,
        });
      }
      //映射新增
      function handleCreateMappingField(fieldType) {
        // 1是映射 2是多重映射
        openMappingFieldsDrawer(true, {
          isUpdate: false,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
          fieldType,
          dataSourceLength: getDataSource().length,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          moduleCode: props.moduleCode,
          formCode: props.formCode,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await getVisualColDel({
          code: props.moduleCode,
          formCode: props.formCode,
          fieldName: record.field,
        }).then(() => {
          handleSuccess();
        });
      }

      function handleSuccess() {
        reload();
        viewColumnsRef.value?.fetch();
      }
      onMounted(() => {});
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
        handleCreateMappingField,
        registerMappingFieldsDrawer,
        viewColumnsRef,
      };
    },
  });
</script>
